package com.lanou.liuqixin.datebasedemo.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;


import com.lanou.liuqixin.datebasedemo.BuildConfig;
import com.lanou.liuqixin.datebasedemo.R;
import com.lanou.liuqixin.datebasedemo.adapter.MainFragmentAdapter;
import com.lanou.liuqixin.datebasedemo.fragment.FyongFragment;
import com.lanou.liuqixin.datebasedemo.adapter.ListViewAdapter;
import com.lanou.liuqixin.datebasedemo.fragment.PmingFragment;
import com.lanou.liuqixin.datebasedemo.fragment.UfeiFragment;
import com.lanou.liuqixin.datebasedemo.fragment.UhaoFragment;

import java.util.ArrayList;
import java.util.List;

import db.CarEntity;
import db.DatabaseTool;


public class MainActivity extends AppCompatActivity {
    private List<Fragment> mFragments;
    private DrawerLayout mDrawerLayout;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private MainFragmentAdapter mMainFragmentAdapter;
    private Spinner mSpinner;
    private List<String> dates;
    private ArrayAdapter<String> madapter;
    private LayoutInflater mLayoutInflater;
    private View mView;
    private ListView mListView;
    private ListViewAdapter mListViewAdapter;
    private ImageView addIv, cheIv;
    private TextView closeTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        创建retrofit对象  ConverterFactory指定解析框架 add可以添加多个
//        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.xiaoxiongyouhao.com/").
//                addConverterFactory(GsonConverterFactory.create()).build();
//        创建BearService对象
//        BearService service = retrofit.create(BearService.class);
//        第一次进行进行网络请求
//        Call<BrandEntity> brands = service.getBrands();
//        brands.enqueue(new Callback<BrandEntity>() {
//            @Override
//            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
//
//                BrandEntity body = response.body();
//
//            }
//
//            @Override
//            public void onFailure(Call<BrandEntity> call, Throwable t) {
//
//            }
//        });

//        根据不同的index获取车系
//        Call<BrandEntity> carSeries =service.getCarSeries(35);
//        carSeries.enqueue(new Callback<BrandEntity>() {
//            @Override
//            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
//
//            }
//
//            @Override
//            public void onFailure(Call<BrandEntity> call, Throwable t) {
//
//            }
//        });
         //调用数据库中添加汽车的方法 在spinner中添加车辆
//        CarEntity car =new CarEntity(4);
//        car.setName("傻狍子");
//        car.setSelected(0);
//        DatabaseTool.getInstance().addCar(car);



        cheIv = (ImageView) findViewById(R.id.one_iv);
        mLayoutInflater = LayoutInflater.from(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        mTabLayout = (TabLayout) findViewById(R.id.main_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.main_vp);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mSpinner = (Spinner) findViewById(R.id.spinner);

//        View view = LayoutInflater.from(this).inflate(R.layout.item_listview, null);
//        deleteIv = (ImageView) view.findViewById(R.id.item_iv);

        initTabLayout();
        initActionBar(toolbar);
        //添加下拉列表项的菜单项
        dates = new ArrayList<>();
        final List<CarEntity> cars = DatabaseTool.getInstance().queryCars();

        for (int i = 0; i < cars.size(); i++) {
            dates.add(cars.get(i).getName());
            Log.d("MainActivity", "车辆数据 id: " + cars.get(i).get_id() + " name: " + cars.get(i).getName());

        }
        dates.add("车辆管理");


        Log.d("MainActivity", "DatabaseTool.getInstance().queryCars().size():" + DatabaseTool.getInstance().queryCars().size());

        //为下拉列表定义一个适配器
        madapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dates);
        //为适配器设置下拉列表下拉时候的菜单样式
        madapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将适配器添加到下拉列表上
        mSpinner.setAdapter(madapter);


        //为下拉列表设置响应事件
        mSpinner.setSelection(0,false);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, final long id) {

                mView = mLayoutInflater.inflate(R.layout.main_custom_view, parent, false);

                mListViewAdapter = new ListViewAdapter(cars, MainActivity.this);

                mListView = (ListView) mView.findViewById(R.id.lv_car_info);
                mListView.setAdapter(mListViewAdapter);
                addIv = (ImageView) mView.findViewById(R.id.add_car_by_dialog);
                closeTv = (TextView) mView.findViewById(R.id.custom_dialog_close);
                mListViewAdapter.setOnDelClickListener(new ListViewAdapter.OnDelClickListener() {
                    @Override
                    public void onDelClick(CarEntity car) {
                        DatabaseTool.getInstance().removeCar(car.get_id());
                        ArrayAdapter adapter = (ArrayAdapter) mSpinner.getAdapter();
                        adapter.remove(car.getName());
                    }
                });
                addIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, RetroitActivity.class);
                        startActivity(intent);
                    }
                });


                if (position == parent.getCount() - 1) {
                    //设置dialog
                    //dialog构造器
                    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    mSpinner.setSelection(0);
                    builder.setView(mView);
                    final AlertDialog dialog = builder.create();
                    dialog.show();
                    closeTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            builder.setCancelable(true);
                            dialog.dismiss();
                        }
                    });


                }

            }


            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });


        //下拉菜单弹出的内容选项触屏事件处理
        mSpinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mSpinner.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


                return false;
            }
        });

        cheIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CheckedBoxActivity.class);
                startActivity(intent);
            }
        });

        if (BuildConfig.DEBUG)
            Log.d("balalaxiaomoxian", "车辆信息:" + DatabaseTool.getInstance().queryCars());




    }


    private void initActionBar(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.login_bear);
        }
    }

    private void initTabLayout() {
        mFragments = new ArrayList<>();
        mFragments.add(new UhaoFragment());
        mFragments.add(new PmingFragment());
        mFragments.add(new UfeiFragment());
        mFragments.add(new FyongFragment());
        mMainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mMainFragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        for (int i = 0; i < mMainFragmentAdapter.getCount(); i++) {
            View tabView = mMainFragmentAdapter.getTabView(i, this);
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(tabView);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

}

